package org.zsk.cfl.listerner;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.text.NumberFormat;

/**
 * <p>
 *     多实例完成条件判断
 * </p>
 *
 * @author zsk
 * @date 2020/11/10 上午8:11
 */
@Component("multiInstCompleteCondition")
@Slf4j
public class MultiInstanceCompleteCondition implements Serializable {

    @Autowired
    RuntimeService runtimeService;

    /**
     * 按比例通过结果计算
     * @param execution 委派执行实例
     * @return 同意比例
     */
    public double passWithProportion(DelegateExecution execution) {
//        //已完成的实例数
//        int nrOfInstances= (int) execution.getVariable(MultiConstants.NR_OF_INSTANCES);
//        //同意票数量
//        int accepted=(int) execution.getVariable(MultiConstants.ACCEPTED);
//        //计算通过比例
//        NumberFormat numberFormat = NumberFormat.getInstance();
//        numberFormat.setMaximumFractionDigits(2);
//        String result = numberFormat.format((float)accepted/(float)nrOfInstances);
//        Float percentageOfAccepted=Float.parseFloat(result);
//        log.debug("按比例通过的通过比例：{}",percentageOfAccepted);
//        //设置同意比例变量
//        execution.setVariable("percentageOfAccepted", percentageOfAccepted);
        float percentageOfAccepted=0.3f;
        return percentageOfAccepted;
    }
}
